package com.sanpu.kxe.business.main.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.main.bean.HomeTradeOutput;
import com.sanpu.kxe.business.main.interfaces.IOnChangeHomeListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Description：
 * Created by 薛勇军 on 2019/9/18.
 */

public class HomeTradeAdapter extends PagerAdapter {

    private Context mContext;

    private List<HomeTradeOutput> mTradeOutputs = new ArrayList<>();

    private IOnChangeHomeListener mHomeListener;

    public HomeTradeAdapter(Context mContext, IOnChangeHomeListener homeListener) {
        this.mContext = mContext;
        this.mHomeListener = homeListener;
    }

    /**
     * 刷新数据
     */
    public void setNotifyData(List<HomeTradeOutput> rateOutputs) {
        if (rateOutputs != null) {
            mTradeOutputs.clear();
            mTradeOutputs.addAll(rateOutputs);
        }
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mTradeOutputs == null)
            return 0;
        return mTradeOutputs.size();
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_home_trade, null);
        TextView titleTv = ButterKnife.findById(view, R.id.titleTv);
        TextView amountTv = ButterKnife.findById(view, R.id.amountTv);
        TextView remarkTv = ButterKnife.findById(view, R.id.remarkTv);
        ImageView showIv = ButterKnife.findById(view, R.id.showIv);

        final HomeTradeOutput tradeOutput = mTradeOutputs.get(position);
        if (tradeOutput != null) {
            if (!TextUtils.isEmpty(tradeOutput.getTitle())) {
                titleTv.setText(tradeOutput.getTitle());
            }

            if (!TextUtils.isEmpty(tradeOutput.getRemark())) {
                remarkTv.setText(tradeOutput.getRemark());
            }
            if (tradeOutput.isShow()) {
                if (!TextUtils.isEmpty(tradeOutput.getMoney())) {
                    amountTv.setText(tradeOutput.getMoney());
                }
                showIv.setImageResource(R.drawable.ic_home_head_show);
            } else {
                amountTv.setText("******");
                showIv.setImageResource(R.drawable.ic_home_head_close);
            }
            showIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mHomeListener.onChangeShow(tradeOutput.isShow(), position);
                }
            });
        }

        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
